package com.exposit.zbs.controller;

import com.exposit.zbs.model.GrantApplication;
import com.exposit.zbs.model.User;
import com.exposit.zbs.service.IGrantApplicationService;
import com.exposit.zbs.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
@RequestMapping("/profile")
public class ProfileController {

	@Autowired
	private IUserService userService;
	@Autowired
	private IGrantApplicationService grantAppService;

	@RequestMapping(value = "/{userId}", method = RequestMethod.GET)
	public ModelAndView showUserProfile(@PathVariable String userId, @PathVariable("userId") User userModelTest, HttpSession session, Model model) {

		ModelAndView mav = new ModelAndView();
		mav.setViewName("profile");
		mav.addObject("user", userModelTest);

		return mav;
	}

	@RequestMapping(value = "/grants/viewAll", method = RequestMethod.GET)
	public ModelAndView viewAllGrants(HttpSession session) {

		ModelAndView mav = new ModelAndView();
		Object currentUserObject = session.getAttribute("sessionUser");
		if (currentUserObject != null && currentUserObject instanceof User) {
			User currentUser = (User) currentUserObject;
			List<GrantApplication> grantApplications = grantAppService.getAllGrants();
			mav.setViewName("profile.grants.viewAll");
			if (grantApplications != null && grantApplications.size() > 0) {
				mav.addObject("grantApplications", grantApplications);
				mav.addObject("newGrantApplicationa", new GrantApplication());
			} else {
				mav.addObject("message", "Nothing to view");
			}
			return mav;
		}
		mav = new ModelAndView("redirect:/");
		return mav;
	}

	@RequestMapping(value = "/grants/viewAll", method = RequestMethod.POST)
	public ModelAndView saveGrantApplication(GrantApplication grantApplication, HttpSession session) {

		ModelAndView mav = new ModelAndView();
		Object currentUserObject = session.getAttribute("sessionUser");
		if (currentUserObject != null && currentUserObject instanceof User) {
			User currentUser = (User) currentUserObject;
			grantAppService.saveGrant(grantApplication);
			List<GrantApplication> grantApplications = grantAppService.getAllGrants();
			mav.setViewName("profile.grants.viewAll");
			if (grantApplications != null && grantApplications.size() > 0) {
				mav.addObject("grantApplications", grantApplications);
				mav.addObject("newGrantApplicationa", new GrantApplication());
			} else {
				mav.addObject("message", "Nothing to view");
			}
			return mav;
		}
		mav = new ModelAndView("redirect:/");
		return mav;
	}

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public ModelAndView editMyProfile(HttpSession session) {

		ModelAndView mav = new ModelAndView("profile.edit");
		Object currentUserObject = session.getAttribute("sessionUser");
		if (currentUserObject != null && currentUserObject instanceof User) {
			User currentUser = (User) currentUserObject;
			mav.addObject("user", currentUser);
		} else {
			return new ModelAndView("redirect:/login");
		}
		return mav;
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public ModelAndView saveMyProfile(User user, HttpSession session) {

		ModelAndView mav = new ModelAndView("profile.edit");
		Object currentUserObject = session.getAttribute("sessionUser");
		if (currentUserObject != null && currentUserObject instanceof User) {
			User currentUser = (User) currentUserObject;
			user.setUserId(currentUser.getUserId());
			user.setRole(currentUser.getRole());
			userService.updateUserData(user);

			mav.addObject("user", user);
			mav.addObject("message", "Setting changed sucessfully.");
		} else {
			return new ModelAndView("redirect:/login");
		}
		return mav;
	}

}
